package com.app.brilliant.system.scheduled;
import com.github.pagehelper.PageInfo;
import com.app.brilliant.system.entity.ScheduleJob;
import com.app.brilliant.system.service.ScheduleJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class BootRunSchedule implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private ScheduleJobService scheduleJobService;
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        if (contextRefreshedEvent.getApplicationContext().getParent() == null) {//保证只执行一次
            Map<String,Object> info = new HashMap<String,Object>();
            info.put("beanName","ScheduleRunBean");
            PageInfo pageInfo = scheduleJobService.queryPage(info);
            List<Object> list = pageInfo.getList();
            List<Long> ids = list.stream().filter(obj->obj instanceof ScheduleJob).map(y->{
                ScheduleJob scheduleJob = (ScheduleJob) y;
                return scheduleJob.getJobId();
            }).collect(Collectors.toList());
            scheduleJobService.resume(ids);
        }
    }
}
